package com.fortune.service.impl;

import com.fortune.constants.enums.ProductStock;
import com.fortune.dao.MProductStockDetailInfoDao;
import com.fortune.dao.MStoreInitDao;
import com.fortune.dto.in.mStoreInit.MStoreInitAddDto;
import com.fortune.dto.in.mStoreInit.MStoreInitEditDto;
import com.fortune.dto.in.mStoreInit.MStoreInitListDto;
import com.fortune.dto.in.mStoreInit.MStoreInitProduceDto;
import com.fortune.service.MStoreInitService;
import com.fortune.vo.mStoreInit.MInitInfo;
import com.fortune.vo.mStoreInit.MinitDetails;
import com.fortune.vo.productStockDetailInfo.ProductStockDetailInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service
public class MStoreInitServiceImpl implements MStoreInitService {
    @Autowired
    private MStoreInitDao mStoreInitDao;
    @Autowired
    private MProductStockDetailInfoDao mProductStockDetailInfoDao;

    /**
     * 查询初始化单列表
     * @param mStoreInitListDto
     * @return
     */
    @Override
    public List<MInitInfo> queryMInitInfoList(String companyId,MStoreInitListDto mStoreInitListDto) {
        return mStoreInitDao.queryMInitInfoList(companyId,mStoreInitListDto);
    }

    /**
     * 查询初始化单列表个数
     * @return
     */
    @Override
    public int queryMInitInfoListCount(String companyId) {
        return mStoreInitDao.queryMInitInfoListCount(companyId);
    }

    /**
     * 查询初始化单数据是否存在
     * @param companyId
     * @param num
     * @param storeId
     * @return
     */
    @Override
    public int queryMInitInfoIsExist(String companyId,String num,String storeId) {
        return mStoreInitDao.queryMInitInfoIsExist(companyId,num,storeId);
    }

    /**
     * 添加初始化单
     * @param userId
     * @param companyId
     * @param addDto
     * @return
     */
    @Override
    @Transactional
    public int addMInitInfo(String userId,String companyId, MStoreInitAddDto addDto) {
        String id = UUID.randomUUID().toString().replace("-","");
        int num = mStoreInitDao.addMInitInfo(userId,id,companyId,addDto);
        if (num < 1){
            throw new RuntimeException("添加初始化单失败");
        }
        List<MStoreInitProduceDto> list = addDto.getProduce();
        num = mStoreInitDao.addInitDetailInfo(companyId,id,addDto.getProduce());
        if (num < 1){
            throw new RuntimeException("添加初始化单失败");
        }
        return 1;
    }

    /**
     * 编辑初始化单
     * @param companyId
     * @param editDto
     * @return
     */
    @Override
    @Transactional
    public int editMInitInfo(String companyId, MStoreInitEditDto editDto) {
        mStoreInitDao.editMInitInfo(companyId,editDto);
        mStoreInitDao.deleteMInitDetailInfo(companyId,editDto.getId());
        int num = mStoreInitDao.editInitDetailInfo(companyId,editDto.getProduce(),editDto.getId());
        if (num < 1){
            throw new RuntimeException("编辑初始化单失败");
        }
        return 1;
    }

    /**
     * 删除初始化单
     * @param companyId
     * @param id
     * @return
     */
    @Override
    public int deleteMInitInfo(String companyId, String id) {
        return mStoreInitDao.deleteMInitInfo(companyId,id);
    }

    /**
     * 确认初始化单
     * @param companyId
     * @param id
     * @return
     */
    @Override
    @Transactional
    public int storeInitConfirm(String userId, String companyId, String id) {
        int num = mStoreInitDao.updateStoreInit(userId,companyId,id);
        if (num < 1){
            throw new RuntimeException("确认初始化单失败");
        }
        List<ProductStockDetailInfo> list = mStoreInitDao.queryProductStock(companyId,id);
        for (int i = 0; i < list.size(); i++){
            ProductStockDetailInfo detailInfo = list.get(i);
            int order = mProductStockDetailInfoDao.queryMax(companyId,detailInfo.getStoreId(),detailInfo.getMpid());
            List<ProductStockDetailInfo> lastDate = mProductStockDetailInfoDao.queryLastStockDetail(companyId,detailInfo.getStoreId(),detailInfo.getMpid());
            if (!lastDate.isEmpty()){
                detailInfo.setRemainNum(lastDate.get(0).getRemainNum()+detailInfo.getInNum());
            }else {
                detailInfo.setRemainNum(detailInfo.getInNum());
            }
            num = mProductStockDetailInfoDao.insertSingleInProductStockDetail(companyId,ProductStock.CSHRK,detailInfo,order);
            if (num < 1){
                throw new RuntimeException("确认初始化单失败");
            }
        }
        return 1;
    }

    /**
     * 获取初始化单详情
     * @param companyId
     * @param id
     * @return
     */
    @Override
    public MinitDetails queryStoreInitDetails(String companyId, String id) {
        return mStoreInitDao.queryStoreInitDetails(companyId,id);
    }

    /**
     * 编辑时查询初始化单数据是否存在
     * @param companyId
     * @param num
     * @param storeId
     * @return
     */
    @Override
    public int queryEditMInitInfoIsExist(String companyId, String num, String storeId) {
        return mStoreInitDao.queryEditMInitInfoIsExist(companyId,num,storeId);
    }
}
